﻿using ctolua.sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ctolua.Models.System

{ 
    public class Stick
    {

        public static Dictionary<string, Action<string>> ticks = new Dictionary<string, Action<string>>();
        public Stick()
        {
            py.tick(() =>
            {
                foreach (var tick in ticks)
                {
                    try
                    {
                        tick.Value.Invoke(tick.Key);
                    }
                    catch (Exception)
                    {

                    }
                }
            });
        }
        /// <summary>
        /// 添加帧回调
        /// </summary>
        /// <param name="id"> 回调指定id </param>
        /// <param name="action"></param>
        public static string add(string id, Action<string> action)
        {
            ticks.Add(id, action);
            return id;
        }
        /// <summary>
        /// 添加帧回调 
        /// </summary>
        /// <param name="action"> id </param>
        public static string add(Action<string> action)
        {
            string id = SUuid.uuid();
            ticks.Add(id, action);
            return id;
        }
        /// <summary>
        /// 修改 
        /// </summary>
        /// <param name="action"> id </param>
        public static void revise(string id,Action<string> action)
        {
            if (ticks.ContainsKey(id))
            {
                ticks[id] = action;
            }
        }
        public static bool is_null(string id)
        {
            return ticks.ContainsKey(id);
        }
        /// <summary>
        /// 删除回调
        /// </summary>
        /// <param name="id"></param>
        public static void del (string id)
        {
            ticks.Remove(id);
        }
    }
}
